$(document).ready(function() {
	
	$(".blogOptions").click(function() {
		var id = $(this).attr('id');
		
		var options_arr = id.split("_");
		
		var URL;
		var message;
		var endMessage;
		if (options_arr[0] == "editBlog") {
			URL = "/blogs/editBlog/" + options_arr[1];
			message = "Edit Blog?";
		}
		else if(options_arr[0] == "deleteBlog") {
			URL = "/blogs/deleteBlog/" + options_arr[1];
			message = "Delete Blog?"
			endMessage = "Blog " + options_arr[1] + " deleted.";	
		}
		

		if ( confirm(message) ) {
			/*  removing the table row where the blog resides,
			 *  not exactly perfect, but it works. 
			 *  Line below was not working upon ajax success, hence moved it here
			 */
			$(this).parent().parent().parent().remove();
			$.ajax({
				type: 'POST',
				url: URL,
				//data: {'param': blogID},
				success: function() {
					if (options_arr[0] == "deleteBlog"){
						//$(this).parent().parent().parent().remove();
						alert(endMessage);
					}
					else if (options_arr[0] == "editBlog") {
						window.location = URL;
					}		
				},
				error: function() {
					alert('Oops! Try Again Please');
				}
			});
		}
		
	}); //end blogOptions click event
});
